package week4;


import jsjf.exceptions.ElementNotFoundException;
import jsjf.exceptions.EmptyCollectionException;
import week2.LinearNode;

public class ProgramOfStudy1 {
    protected int count;
    protected LinearNode<Course> head, tail;

    // 构造一个最初空的学习项目
    public ProgramOfStudy1()
    {
        count = 0;
        head = tail = null;
    }

    public void addCourse(Course course)
    {
        LinearNode<Course> current = head;
        LinearNode<Course> previous = null;
        LinearNode<Course> node = new LinearNode<>(course);


        while (( current != null) && (course.compareTo(current.getElement()) > 0))
        {
            previous = current;
            current = current.getNext();
        }

        if (previous == null )
        {
            head = node;
        }
        else {
            previous.setNext(node);
        }

        node.setNext(current);

        count++;
    }

    public Course removeFirst() {
        if(isEmpty())
        {
            throw new EmptyCollectionException("LinkedList");
        }

        LinearNode<Course> current = head;
        head = current.getNext();

        count--;

        return current.getElement();
    }

    public Course removeLast() {
        LinearNode<Course> previous = null;
        LinearNode<Course> current = head;

        if(isEmpty())
        {
            throw new EmptyCollectionException("LinkedList");
        }
        while (current != null)
        {
            previous = current;
            current = current.getNext();
        }

        tail = previous;
        tail.setNext(null);

        count--;

        return current.getElement();
    }

    public Course remove(Course targetelement) throws EmptyCollectionException, ElementNotFoundException {
        if(isEmpty())
        {
            throw new EmptyCollectionException("LinkedList");
        }

        boolean found = false;
        LinearNode<Course> previous = null;
        LinearNode<Course> current = head;

        while (current != null && !found)
        {
            if (targetelement.equals(current.getElement()))
            {
                found = true;
            }
            else
            {
                previous = current;
                current = current.getNext();
            }
        }

        if (!found)
        {
            throw new ElementNotFoundException("LinkedList");
        }

        if (size()==1)  // 列表中只有一个元素
        {
            head = tail = null;
        }
        else if (current.equals(tail))  //  目标在头
        {
            head = current.getNext();
        }
        else if (current.equals(tail))  //  目标在尾
        {
            tail = previous;
            tail.setNext(null);
        }
        else   // 目标在中间
        {
            previous.setNext(current.getNext());
        }

        count--;
        return current.getElement();
    }

    public Course first() {
       Course result = head.getElement();
        return result;
    }

    public Course last() {
        Course result = tail.getElement();
        return result;
    }

    public boolean contains(Course target) {
        LinearNode<Course> current = head;
        while (current != null)
        {
            if (target.equals(current.getElement()))
            {
                return true;
            }
        }
        return false;
    }

    public boolean isEmpty() {
        return count == 0;
    }

    public int size() {
        return count;
    }

    public String toString()
    {
        LinearNode<Course> temp;
        temp = head;
        String result = "";
        while(temp != null)
        {
            result += temp.getElement() + "\n";
            temp = temp.getNext();
        }
        return result;
    }

}
